<?php
  class teacher_actions{
       function __construct($teacher_id="0"){
         $this->teacher_id=$teacher_id;
     }
     
     function create_ledger(){
         #this function createas a new teacher ledger
         sql_functions::apply_query("create table teacher_".$this->teacher_id." like teacher_no");
     }
     function insert_to_ledger($data){
         #this function inserts to the profile
         sql_functions::apply_query("insert into teacher_profile(name,teacher_id,last_name,first_name,middle_name)values('".strtoupper($data['lname']).","." ".strtoupper($data['fname'])." ".strtoupper(substr($data['mname'],0,1))."','".strtoupper($data['teacher_id'])."','".strtoupper($data['lname'])."','".strtoupper($data['fname'])."','".strtoupper(substr($data['mname'],0,1))."')");
     }
     
     function check_table_exists($data){
         #this function checks whether there is already a table taht exists
          $table_exists = sql_functions::select_query("count(table_name) as bilang from tables where table_schema='ssc_reg_sis' and table_name='teacher_".$data['teacher_id']."'","information_schema");                 
              if(sql_functions::count_rows($table_exists)==0){
                  return false;
              }else{
                  return true;
              }
     }
     
     function check_my_student($data){
         #this function will check the id no of student if enrolled to the teacher
         
     }
     
     function get_my_students($data){
         #this function will get the students of teachers
         #1. preparet the courses
         $courses=array();
         foreach($data['course'] as $course){
             array_push($courses,"'".$course."'");
         }
         $courses = implode(",",$courses);
         $query = sql_functions::select_query("una.student_id,duwa.name,duwa.course,duwa.major FROM enroll_".$_SESSION['syear']." as una left join reg_profile as duwa on una.student_id=duwa.stud_id where concat(una.course,'-',una.major) in (".$courses.") and una.yr_lvl='".$data['yr_lvl']."' and una.semester='".$data['semester']."' and una.subject like '%".$data['subj_code']."%' and una.section='".$data['section']."'");
         $students = sql_functions::fetch_to_array($query);
         return $students;
     }
     
     function insert_to_records($data){
         $header= "insert into teacher_".$_SESSION['profile_id']."(student_id,subj_code,taken)values";
         foreach($data['student_id'] as $datum){
            $body .= ("('".$datum."','".$data['subject']."','".$_SESSION['syear']."-".$data['semester']."-".$data['section']."')");

         }
         #prepare the header
         $body = str_replace(")(","),(",$body);
         $query = $header.$body;
         sql_functions::apply_query($query);
     }
     
     function update_student_instructor($student){
        foreach($student['student_id'] as $students){
            sql_functions::apply_query("update stud_".$students." set ins_1=concat(ins_1,'|',".$_SESSION['profile_id'].") where subj_code='".$student['subject']."'");
        } 
     }
     
     function update_grade($data){
         $query = sql_functions::select_query("* from teacher_".$_SESSION['profile_id']." where subj_code='".$data['subj_code']."' and student_id='".$data['student_id']."' and taken like '".$_SESSION['syear']."%'");
         if(sql_functions::count_rows($query)==0){
             die("<p class='warnings'>You have no exisitng student in your records having id no: ".$ata['student_id']."</p>");
         }
         sql_functions::apply_query("update stud_".$data['student_id']." set grade_1=concat(grade_1,'".$data['grade']."','|'),status='".$data['remarks']."' where subj_code='".$data['subj_code']."'");
     }
     
     function get_list_subjects(){
         $query = sql_functions::select_query("distinct(sujc_code) where taken like '".$_SESSION['syear']."-%"."'");
         $row = sql_functions::fetch_to_array($query);
         return $row;
     }
  }
?>
